//
//  YingDanViewController.m
//  C17
//
//  Created by dllo on 16/3/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YingDanViewController.h"
#import "ZuoZheCollectionViewCell.h"
#import "DianYingCollectionViewCell.h"
#import "DianyingImageCollectionViewCell.h"
#import "DianYingTableViewCell.h"

@interface YingDanViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collection;
@property(nonatomic,retain)NSDictionary *dic;
@property(nonatomic,retain)NSArray *arrAll;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowlayout;

@end

@implementation YingDanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self getData];
    self.title=@"影单";

    self.navigationController.navigationBar.translucent=YES;
    UIImage *image=[UIImage imageNamed:@"bg_clear.png"];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:image];
    
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 25, 25);
    [button setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:button];
    
    //self.flowlayout.itemSize=CGSizeMake(WIDTH, HEIGHT);
    
    [self.collection registerNib:[UINib nibWithNibName:NSStringFromClass([ZuoZheCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"collection"];
    [self.collection registerNib:[UINib nibWithNibName:NSStringFromClass([DianYingCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"dianying"];
}
-(void)click:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrAll.count+1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView==self.collection) {
        if(indexPath.row==0){
            ZuoZheCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
            [cell.downImageView sd_setImageWithURL:[NSURL URLWithString:self.dic[@"base_info"][@"cover_url"]]];
           
            cell.YingDan.text=self.dic[@"base_info"][@"name"];
            cell.desc.text=self.dic[@"base_info"][@"intro"];
            [cell.ZuoZheImageView sd_setImageWithURL:[NSURL URLWithString:self.dic[@"base_info"][@"user"][@"avatar_large"]]];
            cell.ZuoZheImageView.layer.cornerRadius=33;
            cell.ZuoZheImageView.layer.masksToBounds=YES;
            cell.ZuoZheName.text=self.dic[@"base_info"][@"user"][@"name"];
            NSString *str=[NSString stringWithFormat:@"共 %@ 部电影",self.dic[@"base_info"][@"movie_count"]];
            cell.movieCount.text=str;
            return cell;
        }
        else{
            DianYingCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"dianying" forIndexPath:indexPath];
            NSDictionary *dic=self.arrAll[indexPath.row-1];
            [cell.MovieImageView sd_setImageWithURL:[NSURL URLWithString:dic[@"poster_url"]]];
            cell.downView.backgroundColor=[UIColor colorWithRed:137/256.0 green:157/256.0 blue:192/256.0 alpha:0.95];
            cell.MovieName.text=dic[@"name"];
            if ([dic[@"directors"] count] == 0) {
                NSString *str=[NSString stringWithFormat:@"导演 : "];
                cell.DaoYan.text=str;
            }
            else{
                NSString *str=[NSString stringWithFormat:@"导演 : %@",dic[@"directors"][0][@"name"]];
                cell.DaoYan.text=str;
            }
            NSString *str1=[NSString stringWithFormat:@"类型 : %@",dic[@"type"]];
            cell.Type.text=str1;
            cell.DianYingCollection.delegate=self;
            cell.DianYingCollection.dataSource=self;
            cell.webViewName=dic[@"name"];
            if (![dic[@"video_url"] isEqualToString:@""]) {
                cell.webViewUrl=dic[@"video_url"];
                [cell.PlayMovie setTitle:@"全片" forState:UIControlStateNormal];
                cell.PlayMovie.hidden=NO;
            }
            else if(![dic[@"preview_video_url"] isEqualToString:@""]){
                cell.videoUrl=dic[@"preview_video_url"];
                [cell.PlayMovie setTitle:@"预告" forState:UIControlStateNormal];
                cell.PlayMovie.hidden=NO;
            }
            else{
                cell.PlayMovie.hidden=YES;
            }
            cell.root=self;
            cell.arr=self.arrAll;
            cell.indexpathCon = indexPath;
            cell.text=dic[@"intro"];
            [cell.tableView reloadData];
            //cell.yOffect = self.collection.contentOffset.x ;
            return cell;
        }
    }
    else{
        DianyingImageCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"DianYingCollection" forIndexPath:indexPath];
        NSDictionary *dic=self.arrAll[indexPath.row-1];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:dic[@"poster_url"]]];
        return cell;
    }
}


-(void)getData{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    NSDictionary *dic=@{@"id":self.pagelist_id};
    [manager POST:@"http://ting.weibo.com/movieapp/pagelist/page" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.dic=[NSDictionary dictionaryWithDictionary:responseObject[@"data"]];
        [self.collection reloadData];
        
        AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
        NSDictionary *dic=@{@"id":self.pagelist_id,@"v":@"1.4.0",@"count":@"24",@"page":@"1",@"os_v":@"4.4.2",@"wm":@"44994_0020",@"d_n":@"H60-L02&aid=01AgSmm1_K9hzlrRD6aHti_0HmXp4AJe6W20h8l57DaXE1si0.",@"from":@"8614095010",@"type":@"page",@"os_n":@"Android",@"ip":@"172.16.18.156"};
        [manager POST:@"http://ting.weibo.com/movieapp/pagelist/recommendmovie" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            self.arrAll=responseObject[@"data"][@"list"];
            NSLog(@"加载成功");
            [self.collection reloadData];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.translucent = YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
